home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Meshwriter / Geo2Any / geo2any.c next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  6.1 KB  |  203 lines

  1. /*
  2. **      $VER: geo2any.c 1.00 (28.3.99)
  3. **
  4. **      Sample converter program which convertes VideoScape ascii (GEO)
  5. **      files with the help of the MeshWriter library.
  6. **
  7. **      Based on the idea from Stephan Bodmer which made not only Geo2Vrml,
  8. **      but the VrmlViewer and VrmlEditor too. Go and get them !!
  9. **
  10. **      Stephan Bielmann
  11. */
  12.  
  13. /*
  14. ** ANSI standart includes
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19.  
  20. #ifndef WITHMWLLIB
  21. #include "/meshlib.h"
  22. #else
  23. #include <meshwriter/meshwriter.h>
  24. #include <pragma/meshwriter_lib.h>
  25. #include <clib/exec_protos.h>
  26. struct MeshWriterBase *MeshWriterBase = NULL;
  27. #endif
  28.  
  29. FILE *fp=NULL;
  30. unsigned long *indexlist=NULL;
  31. ULONG mesh;
  32. TOCLColor colorTbl[] = {
  33. {  0,  0,  0},{  0,  0,178},{  0,178,  0},{  0,178,178},{178,  0,  0},{255,127,255},
  34. {204,153,102},{127,127,127},{  0,  0,  0},{102,102,255},{102,255,102},{  0,255,100},
  35. {255,102,102},{255,204,255},{255,255,  0},{255,255,255}
  36. };
  37. ULONG materials[16];
  38.  
  39. void finish() {
  40.         free(indexlist);
  41.         fclose(fp);
  42.         MWLMeshDelete(mesh);
  43. #ifdef WITHMWLLIB
  44.         CloseLibrary((APTR) MeshWriterBase);
  45. #endif
  46.  
  47.         exit(0);
  48. }
  49.  
  50. void initmaterials() {
  51.         long i;
  52.         
  53.         for(i=0;i<16;i++) {
  54.                 MWLMeshMaterialAdd(mesh,&(materials[i]));
  55.                 MWLMeshMaterialDiffuseColorSet(mesh,materials[i],&(colorTbl[i]));
  56.         }
  57. }
  58.  
  59. void main(int argc, char *argv[]) {
  60.         char header[10];
  61.         unsigned long vertices=0,polygons=0,material=0;
  62.         unsigned long linecounter=0,ret;
  63.         unsigned long points[1000];
  64.         long i;
  65.         STRPTR *fileformats=NULL;
  66.         TOCLVertex v;
  67.  
  68. #ifdef WITHMWLLIB
  69.         if (!(MeshWriterBase = (APTR) OpenLibrary("meshwriter.library", 0)))
  70.         {
  71.                 printf("Could not open the meshwriter.library\n");
  72.                 exit(30);
  73.         }
  74. #endif
  75.     
  76.         printf("\nThis tool is using MeshWriter.library by Stephan Bielmann.\n\n");
  77.  
  78.         if(argc!=4) {
  79.                 printf("Usage %s inputfile outputfile formatid\n\n",argv[0]);
  80.  
  81.                 fileformats=MWL3DFileFormatNamesGet();
  82.                 printf("Valid format id's are :\n");
  83.                 i=0;
  84.                 while(fileformats!=NULL && fileformats[i]!=NULL) {
  85.                         printf("%2ld : %s\n",MWL3DFileFormatIDGet(fileformats[i]),fileformats[i]);
  86.                         i++;
  87.                 }
  88.                 printf("\n");
  89.  
  90.                 exit(0);
  91.         }
  92.  
  93.         mesh=MWLMeshNew();
  94.         if(mesh==0) {
  95.                 printf("Could not creat a new mesh.\n");
  96.                 exit(0);
  97.         }
  98.  
  99.         initmaterials();
  100.  
  101.         fp=fopen(argv[1],"r");
  102.         if(fp==NULL) {
  103.                 printf("Could not open the input file.\n");
  104.                 exit(0);
  105.         }
  106.  
  107.         fscanf(fp,"%4s",header);
  108.         if(strncmp(header,"3DG1",4)!=0) {
  109.                 printf("Inputfile is not a GEO ascii file.\n");
  110.                 finish();
  111.         }
  112.         
  113.         linecounter++;
  114.         printf("Inputfile is a GEO ascii file.\n");
  115.  
  116.         fscanf(fp,"%ld",&vertices);
  117.         if(vertices==0) {
  118.                 printf("No vertices were found in the inputfile.\n");
  119.                 finish();
  120.         }
  121.         
  122.         linecounter++;
  123.         printf("Found %ld vertices.\nProcessing the vertices.\n",vertices);
  124.  
  125.         indexlist=malloc(sizeof(unsigned long)*vertices);
  126.         if(indexlist==NULL) {
  127.                 printf("Not enough memory to allocate the indexlist.\n");
  128.                 finish();
  129.         }
  130.  
  131.         for(i=0;i<vertices;i++,linecounter++) {
  132.                 // GEO z equals to the libraries y
  133.                 if(fscanf(fp,"%f %f %f",&v.x,&v.z,&v.y)==EOF) {
  134.                         printf("Error while reading the vertices, at line %ld.\n",i+1);
  135.                         finish();
  136.                 }
  137.  
  138.         ret=MWLMeshVertexAdd(mesh,&v,&(indexlist[i]));
  139.                 if(ret!=RCNOERROR) {
  140.                         printf("Could not add a new vertex %ld.\n",ret);
  141.                         finish();
  142.                 }
  143.         }
  144.  
  145.         printf("Processing the polygons.\n");
  146.  
  147.         while(fscanf(fp,"%ld",&polygons)!=EOF) {
  148.                 MWLMeshPolygonAdd(mesh,0);
  149.                 // we limit us to 200 points per polygon, as in the library itself
  150.                 for(i=0;i<polygons&&i<200;i++) {
  151.                         if(fscanf(fp,"%ld",&(points[i]))==EOF) {
  152.                                 printf("Error while reading the polygon information, at line %ld.\n",linecounter);
  153.                                 finish();
  154.                         } else {
  155.                                 if(points[i]>=vertices) {
  156.                                         printf("A polygon its vertex index is not valid %ld at line %ld.\n",points[i],linecounter);
  157.                                         finish();
  158.                                 }
  159.                         }
  160.                 }
  161.  
  162.                 // remember, GEO is clockwise, the library counterclockwise
  163.                 for(i=polygons-1;i>=0;i--) {
  164.                         ret=MWLMeshPolygonVertexAssign(mesh,indexlist[points[i]]);
  165.                         if(ret!=RCNOERROR) {
  166.                                 printf("Could not assign a vertex to the polygo %ld.\n",ret);
  167.                                 finish();
  168.                         }
  169.                 }
  170.  
  171.                 if(fscanf(fp,"%ld",&material)==EOF) {
  172.                         printf("Error while reading the material information, at line %ld.\n",linecounter);
  173.                         finish();
  174.                 } else {
  175.                         // we are using only 16 out of 256 colors.
  176.                         MWLMeshPolygonMaterialSet(mesh,materials[material%16]);
  177.                 }
  178.  
  179.                 linecounter++;
  180.         }
  181.  
  182.         printf("Found %ld polygons.\n",linecounter-2-vertices);
  183.  
  184.         printf("Inputfile processed.\n");
  185.  
  186.         printf("Writing the outputfile.\n");
  187.         ret=MWLMeshSave3D(mesh,atoi(argv[3]),argv[2],NULL);
  188.         if(ret!=RCNOERROR) {
  189.                 printf("Could not save the outputfile %ld.\n",ret);
  190.         }
  191.  
  192.         printf("Cleaning up.\n");
  193.         MWLMeshDelete(mesh);
  194.         free(indexlist);
  195.         fclose(fp);
  196.  
  197.         printf("Finished.\n\n");
  198.  
  199. #ifdef WITHMWLLIB
  200.         CloseLibrary((APTR) MeshWriterBase);
  201. #endif
  202. }  
  203.